package View;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.jgroups.JChannel;
import org.jgroups.Message;

import Controller.ClientController;

public class ClientMainFrame extends JFrame implements ActionListener, ListSelectionListener{
	
	final JLabel nameTopic = new JLabel("Name/topic :");
	final JLabel userConnected = new JLabel("Users connected :");
	final JLabel newChatRoom = new JLabel("Create a new chat room :");
	
	final JButton connect = new JButton("Connect to the chat");
	final JButton create = new JButton("Create");
	final JButton disconnect = new JButton("Disconnect");
	
	JScrollPane consoleScroll = new JScrollPane();
	
	JTextField newChatRoomName= new JTextField();
	
	
	JList list;
	JList list1;

	final String userName;
	
	// The list of the chat rooms
    ArrayList<String> chatRoom = new ArrayList<String>();
    
    // The list of the chat rooms
    ArrayList<String> users = new ArrayList<String>();
	
	JPanel contentPane = new JPanel();
	
	JLabel chatRoomName;
	
	
	public ClientMainFrame(String name)
	{				
		this.userName = name;
		
		// Send the information about the user to the server 

		new ClientController(userName);

		
		this.paintFrame();
		
		
		
	}


	public void paintFrame(){
		

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		setSize(1000, 700);
		setTitle("Client");	


		JLabel welcomeMessage = new JLabel("Welcome " + userName + " ! ");
		
		// Aller chercher les chats room 
	    chatRoom.add("Salut");

		list = new JList(chatRoom.toArray());
		
		contentPane.setBorder(new EmptyBorder(10, 10, 10, 10));
		contentPane.setLayout(new BorderLayout(10, 10));
		setContentPane(contentPane);
		
		contentPane.add(welcomeMessage, BorderLayout.NORTH);
		
		
		
		/////////////// Chat rooms /////////////////////////////////////
		
		JPanel chat = new JPanel();
		chat.setLayout(new BorderLayout());

		JScrollPane scrollPane = new JScrollPane(list);
		scrollPane.setBorder(new TitledBorder(new EtchedBorder(), "Available chat rooms : "));
		//scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setPreferredSize(new Dimension(200,100)); 

		contentPane.add(scrollPane, BorderLayout.WEST);
		
		
		
		
		////////////// Commands ///////////////////////////////////////
		
		JPanel commands = new JPanel();
		contentPane.add(commands, BorderLayout.CENTER);
		
		
		///// Chat Room information 
		
		JPanel chatRoomInfo = new JPanel();
		chatRoomInfo.setLayout(new BorderLayout());
		chatRoomInfo.setBorder(new TitledBorder(new EtchedBorder(),
				"Information about the selected room : "));
		chatRoomInfo.setPreferredSize(new Dimension(550,200)); 
		
		
		// Add the name of the selected chat room
		
		JPanel info = new JPanel();
		info.setBorder(new EmptyBorder(10, 10, 0, 0));
		
		info.setLayout(new BorderLayout(0,-50));
		chatRoomInfo.add(info, BorderLayout.CENTER);
		
		info.add(nameTopic, BorderLayout.NORTH);
		chatRoomName = new JLabel();
		
		info.add(userConnected, BorderLayout.CENTER);

		
		/// Connect to the chat room 	
			
		JPanel flowPanel = new JPanel(new FlowLayout());
		flowPanel.add(connect);

		chatRoomInfo.add(flowPanel, BorderLayout.SOUTH);

		commands.add(chatRoomInfo, BorderLayout.NORTH);
		
		
		/////// Open a new chat room 
		
		JPanel newPanelChatRoom = new JPanel();
		newPanelChatRoom.setPreferredSize(new Dimension(550,30));
		newPanelChatRoom.setLayout(new BorderLayout(0,0));

		newPanelChatRoom.add(newChatRoom, BorderLayout.WEST);
		newPanelChatRoom.add(newChatRoomName, BorderLayout.CENTER);
		newPanelChatRoom.add(create, BorderLayout.EAST);
		
		commands.add(newPanelChatRoom, BorderLayout.CENTER);
		
		
		
		
		////////// Console //////////////////

		JPanel consoleButton = new JPanel();
		consoleButton.setLayout(new BorderLayout());
		
		JPanel console = new JPanel();
		console.setPreferredSize(new Dimension(550,387));
		
		console.setLayout(new BorderLayout());
		console.setBorder(new TitledBorder(new EtchedBorder(), "Console : "));
		
		consoleButton.add(console, BorderLayout.CENTER);
		
		commands.add(consoleButton, BorderLayout.SOUTH);
		


		consoleScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

		console.add(consoleScroll, BorderLayout.CENTER);

		
		/////////////////////// Users connected ////////////////////////////////////////
		
		users.add("User1");
		users.add("User2");

		list1= new JList(users.toArray());
		
		JPanel users = new JPanel();
		users.setLayout(new BorderLayout());

		JScrollPane scrollPane1 = new JScrollPane(list1);
		scrollPane1.setBorder(new TitledBorder(new EtchedBorder(), "Users : "));
		//scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane1.setPreferredSize(new Dimension(200,100)); 


		users.add(scrollPane1, BorderLayout.CENTER);

		JPanel flowPanel1 = new JPanel(new FlowLayout());
		flowPanel1.add(disconnect);
		users.add(flowPanel1, BorderLayout.SOUTH);

		users.add(disconnect, BorderLayout.SOUTH);

		contentPane.add(users, BorderLayout.EAST);

		
	
		setLocationRelativeTo(null);

		setVisible(true);	
		
		
		
		
		connect.addActionListener(this);
		create.addActionListener(this);
		disconnect.addActionListener(this);
	}
	
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		
		JButton btn = (JButton) arg0.getSource();
		
		if(btn == connect){
			
			
		}
		else if (btn == create){
			
			
		}
		else if (btn == disconnect){
			
			
		}
	}



	@Override
	public void valueChanged(ListSelectionEvent arg0) {
		// TODO Auto-generated method stub
		
		JList lis = (JList) arg0.getSource();
		
		if(lis == list){
			
			
		}
		else if (lis == list1){
			
			
		}
		
		
	}



}
